package com.king.juc.shared;

import lombok.extern.slf4j.Slf4j;

/**
 * 线程八锁
 *
 * @author king
 * @date 2020/10/28 14:57
 */
@Slf4j(topic = "c.ThreadEightLock")
public class ThreadEightLock {

    public static void main(String[] args) {
        lock();
    }

    public static void lock() {
        Number n1 = new Number();
        Number n2 = new Number();
        new Thread(() -> {
            log.debug("begin");
            n1.a();
        }).start();
        new Thread(() -> {
            log.debug("begin");
//            n1.b();
            n2.b();
        }).start();
//        new Thread(() -> {
//            log.debug("begin");
//            n1.c();
//        }).start();
    }

    /**
     * 12 | 21 两种情况
     */
    @Slf4j(topic = "c.Number")
    static class Number1{
        public synchronized void a(){
            log.debug("1");
        }
        public synchronized void b(){
            log.debug("2");
        }
    }

    /**
     * 1s后 12 | 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number2 {
        public synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public synchronized void b() {
            log.debug("2");
        }
    }

    /**
     * 3 1s后 12
     * 23 1s后 1
     * 32 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number3 {
        public synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public synchronized void b() {
            log.debug("2");
        }

        public void c() {
            log.debug("3");
        }
    }

    /**
     * 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number4 {
        public synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public synchronized void b() {
            log.debug("2");
        }
    }

    /**
     * 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number5 {
        public static synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public synchronized void b() {
            log.debug("2");
        }
    }

    /**
     * 1s后 12 | 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number6 {
        public static synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public static synchronized void b() {
            log.debug("2");
        }
    }

    /**
     * 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number7 {
        public static synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public synchronized void b() {
            log.debug("2");
        }
    }

    /**
     * 1s后 12 | 2 1s后 1
     */
    @Slf4j(topic = "c.Number")
    static class Number {
        public static synchronized void a() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            log.debug("1");
        }

        public static synchronized void b() {
            log.debug("2");
        }
    }
}
